﻿using CardMonitor.Model;
using CardMonitor.ViewModel;
using CardMonitor.Windows;
using DotNetSpeech;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// ReplaceCard.xaml 的交互逻辑
    /// </summary>
    public partial class ReplaceCard : System.Windows.Controls.Page
    {
        SpVoice speech = new SpVoice();
        SynchronizationContext _syncContext = null;
        IndexViewModel viewModel;
        MainWindow mainWindow = null;
        private Action<int, String> callbackAction;
        KeyBoardWindow inputWindow = null;
        public ReplaceCard()
        {
            InitializeComponent();
        }
        public ReplaceCard(MainWindow _win)
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            viewModel = new IndexViewModel();
            viewModel.index = 0;
            mainWindow = _win;
        }
        private void goIndex(object sender, RoutedEventArgs e)
        {
          

            this.NavigationService.GoBack();
        }
        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void SelectCarNum(object sender, RoutedEventArgs e)
        {
            string carNum = SearchBox.Text;

            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
             paramJSON.Add("carNumber", carNum);
            //paramJSON.Add("carNumber", "鲁P22222");
            httpPostWindow.setParamObject(paramJSON);
            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillByCarNum.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "未查询到单据";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            JArray resultArray = httpPostWindow.getResultArray();
            List<BillModel> billModel = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            SaleCards card = new SaleCards(billModel, mainWindow);
            NavigationService.GetNavigationService(this).Navigate(card);
        }
    }
}
